<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( "system" );
jieqi_checkpower( $jieqiPower['system']['adminconfig'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_loadlang( "groups", JIEQI_MODULE_NAME );
include_once( JIEQI_ROOT_PATH."/lib/html/formloader.php" );
include_once( JIEQI_ROOT_PATH."/class/groups.php" );
$groups_handler =& jieqigroupshandler::getinstance( "JieqiGroupsHandler" );
if ( empty( $_REQUEST['action'] ) )
{
		$GLOBALS['_REQUEST']['action'] = "show";
}
switch ( $_REQUEST['action'] )
{
case "new" :
		if ( empty( $_POST['groupname'] ) )
		{
				jieqi_printfail( $jieqiLang['system']['need_group_name'] );
		}
		else
		{
				$groups = $groups_handler->create( );
				$groups->setvar( "name", $_POST['groupname'] );
				$groups->setvar( "description", $_POST['description'] );
				$groups->setvar( "grouptype", "0" );
				if ( $groups_handler->insert( $groups ) )
				{
						break;
				}
				jieqi_printfail( $jieqiLang['system']['add_group_failure'] );
		}
		break;
case "delete" :
		if ( empty( $_REQUEST['id'] ) )
		{
				break;
		}
		$groups_handler->delete( $_REQUEST['id'] );
		break;
case "update" :
		if ( empty( $_REQUEST['id'] ) || empty( $_POST['groupname'] ) )
		{
				break;
		}
		$groups = $groups_handler->get( $_REQUEST['id'] );
		if ( !is_object( $groups ) )
		{
				break;
		}
		$groups->setvar( "name", $_POST['groupname'] );
		$groups->setvar( "description", $_POST['description'] );
		if ( $groups_handler->insert( $groups ) )
		{
				break;
		}
		jieqi_printfail( $jieqiLang['system']['edit_group_failure'] );
		break;
case "edit" :
		if ( empty( $_REQUEST['id'] ) )
		{
				break;
		}
		$groups = $groups_handler->get( $_REQUEST['id'] );
		if ( !is_object( $groups ) )
		{
				break;
		}
		include_once( JIEQI_ROOT_PATH."/admin/header.php" );
		$groups_form = new jieqithemeform( $jieqiLang['system']['edit_group'], "groupsedit", JIEQI_URL."/admin/groups.php" );
		$groups_form->addelement( new jieqiformtext( $jieqiLang['system']['table_groups_groupname'], "groupname", 30, 50, $groups->getvar( "name", "e" ) ), true );
		$groups_form->addelement( new jieqiformtextarea( $jieqiLang['system']['table_groups_description'], "description", $groups->getvar( "description", "e" ), 5, 50 ) );
		$groups_form->addelement( new jieqiformhidden( "action", "update" ) );
		$groups_form->addelement( new jieqiformhidden( "id", $_REQUEST['id'] ) );
		$groups_form->addelement( new jieqiformbutton( "&nbsp;", "submit", LANG_SAVE, "submit" ) );
		$jieqiTpl->assign( "jieqi_contents", "<br />".$groups_form->render( JIEQI_FORM_MIDDLE )."<br />" );
		include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
		exit( );
}
include_once( JIEQI_ROOT_PATH."/admin/header.php" );
$criteria = new criteriacompo( );
$criteria->setsort( "groupid" );
$criteria->setorder( "ASC" );
$groups_handler->queryobjects( $criteria );
$groups = array( );
$groupary = array( );
$i = 0;
while ( $v = $groups_handler->getobject( ) )
{
		$groupary[$v->getvar( "groupid" )] = $v->getvar( "name" );
		$groups[$i]['groupid'] = $v->getvar( "groupid" );
		$groups[$i]['name'] = $v->getvar( "name" );
		$groups[$i]['description'] = $v->getvar( "description" );
		$groups[$i]['grouptype'] = $v->getvar( "grouptype" );
		++$i;
}
$jieqiTpl->assign_by_ref( "groups", $groups );
$groups_form = new jieqithemeform( $jieqiLang['system']['add_group'], "groupsnew", JIEQI_URL."/admin/groups.php" );
$groups_form->addelement( new jieqiformtext( $jieqiLang['system']['table_groups_groupname'], "groupname", 30, 50, "" ), true );
$groups_form->addelement( new jieqiformtextarea( $jieqiLang['system']['table_groups_description'], "description", "", 5, 50 ) );
$groups_form->addelement( new jieqiformhidden( "action", "new" ) );
$groups_form->addelement( new jieqiformbutton( "&nbsp;", "submit", $jieqiLang['system']['add_group'], "submit" ) );
$jieqiTpl->assign( "form_addgroup", "<br />".$groups_form->render( JIEQI_FORM_MIDDLE )."<br />" );
$jieqiTpl->setcaching( 0 );
$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/groups.html";
include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
if ( ( !empty( $_REQUEST['id'] ) && !empty( $_POST['groupname'] ) ) || 0 < count( $groupary ) )
{
		jieqi_setconfigs( "groups", "jieqiGroups", $groupary, "system" );
		$publicdata = str_replace( "?><?php", "", jieqi_readfile( JIEQI_ROOT_PATH."/configs/system.php" ).jieqi_readfile( JIEQI_ROOT_PATH."/lang/lang_system.php" ).jieqi_readfile( JIEQI_ROOT_PATH."/configs/groups.php" ) );
		jieqi_writefile( JIEQI_ROOT_PATH."/configs/define.php", $publicdata );
}
?>
